<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Tribal extends MY_Controller {
	function __construct(){
		parent::__construct();
	}
	
	public function index(){
		$lastid=$this->input->get('lastid');

		$where=array();
		if(!empty($lastid) && is_numeric($lastid)){
			$where['mw.id <']=(int)$lastid;
		}
		
		$this->Data_model->db->select('mw.*,m.mid,m.mgroup,m.mname,m.mnick,m.avatar')->from('works mw')->join('member m','m.mid=mw.mid','left');
		$mlist = $this->Data_model->getData($where,'mw.puttime DESC,mw.id DESC',8,0);

		$ud=$this->getUnionData($mlist);
		foreach ($mlist as $key => $value) {
			if(empty($value['mnick']))$mlist[$key]['mnick']=$value['mname'];
			$mlist[$key]['content']=empty($value['content'])?'未填写':csubstr($value['content'],45);
			$mlist[$key]['puttime']=date('Y.m.d',$value['puttime']);
			$mlist[$key]['images']=$this->Data_model->getData(array('wid'=>$value['id']),'id ASC',0,5,'work_image');
			if($value['mid']==$this->member->mid){
				$mlist[$key]['spaceurl']=site_url('member');
				$mlist[$key]['href']=site_url('member/work/'.$value['id']);
			}else{
				$mlist[$key]['spaceurl']=site_url('space/'.$value['mid']);
				$mlist[$key]['href']=site_url('space/work/'.$value['mid'].'/'.$value['id']);
			}
			/*$mlist[$key]['works']=$this->Data_model->getData(array(
				'mid'=>$value['mid']
				),'iscomm DESC,id DESC',0,5,'works');*/
			if(!empty($ud['goods'][$value['id']])){
				$mlist[$key]['gooded']=$ud['goods'][$value['id']];
			}
			if(!empty($ud['watchs'][$value['id']])){
				$mlist[$key]['watched']=$ud['watchs'][$value['id']];
			}
		}
		
		$res = array(
			'currentcolumn'=>'tribal',
			'currentnav'=>'骚客部落',
			'mlist'=>$mlist,
			'isajax'=>$this->input->is_ajax_request(),
			'latest'=>$this->getLatest()
		);
		if($res['isajax']){
			$this->view('tribal-ajax',$res);
		}else{
			$this->view('tribal',$res);
		}
	}
}